﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using OT.Data;
using OT.Areas.Instructor.Models;
namespace OT.Areas.Instructor.Controllers
{
    public class HomeController : Controller
    {
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        HttpCookie myCookie1 = new HttpCookie("LoginSes");
        HttpCookie myCookie2 = new HttpCookie("LoginSesID");
        HttpCookie myCookie3 = new HttpCookie("email");
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Isvarify(string dt)
        {
            int cnt=0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
              cnt = OTDb.Users.Where(p => p.UserID.Equals(UserID) && p.IsVarify == false).Count();

            } return Json(cnt, JsonRequestBehavior.AllowGet);
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult fillSub(string dt)
        {
            var subjectList = (List<Subject>)null;
            subjectList = OTDb.Subjects.ToList();
            var stateData = subjectList.Select(m => new SelectListItem()
            {
                Text = m.SubjectName,
                Value = m.SubjectID.ToString(),
            });
            return Json(stateData, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult Topics(int SubjectID)
        {
            var subjectList = (List<Topic>)null;
            subjectList = OTDb.Topics.Where(sub => sub.SubjectID.Value.Equals(SubjectID)).ToList();
            var stateData = subjectList.Select(m => new SelectListItem()
            {
                Text = m.TopicName,
                Value = m.TopicID.ToString(),
            });
            return Json(stateData, JsonRequestBehavior.AllowGet);
        }
        public ActionResult ActivationMail(string dt)
        {
            try
            {
                string Emailreceiver = (string)Request.Cookies["email"].Value ;
                string activationCode = OTDb.Users.Where(u => u.EmailAddress.Trim().Equals(Emailreceiver.Trim())).FirstOrDefault().Guid.Value.ToString();
                string b2 = Request.Url.Authority;
                string b6 = "http://" + b2 + "/Home/SuccessReg?ActivationCode=" + activationCode;
                string body = "Hello " + Request.Cookies["LoginSes"].Value.ToString() + ",";
                body += "<br /><br />Please click the following link to activate your account";
                body += "<br /><a href = '" + b6 + "'>Click here to activate your account.</a>";
                body += "<br /><br />Thanks";
                Utilities.SendEmail(Emailreceiver, "Activation Link for Registration Process", body);
            }
            catch (Exception e)
            {
                Utilities.InsertErrorLog(e);
            }
            return View("index");
        }
        [HttpGet]
        public ActionResult Index()
        {
            Topics topic = null;
            topic = new Topics();
            var sub = OTDb.Subjects.Where(s => s.Status == true).ToList();
            topic.Subjects = sub;
            try
            {
                if (Request.QueryString["Register"] == "sc")
                {
                    Session["sc"] = "Successfuly Registered";
                }
                else
                {
                    Session["sc"] = null;
                }
                if (Request.QueryString["lg"] == "lg")
                {
                    myCookie1.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie1);

                    myCookie2.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie2);

                    myCookie3.Expires = DateTime.Now.AddDays(-1d);
                    Response.Cookies.Add(myCookie3);
                   Response.Redirect("~/home", false);
                }
                else
                {
                    int UserID = 0;
                    if (Request.Cookies["LoginSesID"] != null)
                    {
                        UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                    }
                    else
                    {
                        TempData["notice"] = "Time Out ,Please Login";
                        Response.Redirect("~/Home");
                    }
                    if (ModelState.IsValid)
                    {
                        var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                        if (User != null)
                        {
                            User.LastLoginDate = DateTime.Now;
                            OTDb.SaveChanges();
                         
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View();
        }
        public static List<InstructorChartData_Result> data1obj;

        public JsonResult InstructorBarData(int SubjectId,int TopicID,int CallFor)
        {
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            var labels = OTDb.InstructorChartData(SubjectId, TopicID, CallFor).Select(p => p.FirstName).ToList();
            var Pielabels = OTDb.InstructorPieData(SubjectId, TopicID, CallFor).Select(p => p.FirstName).ToList();

            return Json(new
            {
               labels,Pielabels,
                datasets = new
                {
                    data = OTDb.InstructorChartData(SubjectId, TopicID, CallFor).Select(p => Math.Round((double)p.Average, 2)).ToList(),
                    piedata = OTDb.InstructorPieData(SubjectId, TopicID, CallFor).Select(p =>p.total).ToList(),
                    fillcolor = "rgba(151,187,205,0.3)",
                    strokeColor = "rgba(151,187,205,0.5)",
                    highlightFill = "rgba(151,187,205,0.9)",
                    highlightStroke = "rgba(151,187,205,1)"
                }

            });
        }
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();
            myCookie1.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie1);

            myCookie2.Expires = DateTime.Now.AddDays(-1d);
            Response.Cookies.Add(myCookie2);
            Response.Redirect("~/Login", false);
            return(null);
        }
    }
}
